<?php

/**
 * This is the model class for table "tbl_po_ledger".
 *
 * The followings are the available columns in table 'tbl_po_ledger':
 * @property integer $id
 * @property string $pono
 * @property string $partno
 * @property string $description
 * @property string $qty
 * @property string $price
 * @property string $amount
 * @property string $received
 * @property string $jobno
 * @property string $deptid
 * @property string $jobclassid
 * @property string $costype
 * @property integer $entrytype
 * @property integer $clearned
 * @property string $dispatchno
 * @property string $invoicetransid
 */
class PoLedger extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return PoLedger the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_po_ledger';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            //array('pono, partno, description, qty, price, amount, jobno, deptid, jobclassid, costype, entrytype, clearned, dispatchno, invoicetransid', 'required'),
            array('entrytype, clearned', 'numerical', 'integerOnly' => true),
            array('pono', 'length', 'max' => 20),
            array('partno', 'length', 'max' => 50),
            array('qty, price, amount, costype', 'length', 'max' => 10),
            array('jobno', 'length', 'max' => 41),
            array('deptid, jobclassid, invoicetransid', 'length', 'max' => 36),
            array('dispatchno', 'length', 'max' => 15),
            array('received', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, pono, partno, description, qty, price, amount, received, jobno, deptid, jobclassid, costype, entrytype, clearned, dispatchno, invoicetransid', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'poModel' => array(self::BELONGS_TO, 'Po', 'pono'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'pono' => 'Pono',
            'partno' => 'Partno',
            'description' => 'Description',
            'qty' => 'Qty',
            'price' => 'Price',
            'amount' => 'Amount',
            'received' => 'Received',
            'jobno' => 'Jobno',
            'deptid' => 'Deptid',
            'jobclassid' => 'Jobclassid',
            'costype' => 'Costype',
            'entrytype' => 'Entrytype',
            'clearned' => 'Clearned',
            'dispatchno' => 'Dispatchno',
            'invoicetransid' => 'Invoicetransid',
        );
    }
    public function getReceived(){
      return  $data = Yii::app()->db
        ->createCommand('select SUM(qty) from tbl_inventoryact
        where ponum=:ponum and partno=:partno and type=:type')
            ->bindValues(array(
            'ponum' => $this->pono,
            'partno' => $this->partno,
            'type'=>'I'
        ))
            ->queryScalar();
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('pono', $this->pono, true);
        $criteria->compare('partno', $this->partno, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('qty', $this->qty, true);
        $criteria->compare('price', $this->price, true);
        $criteria->compare('amount', $this->amount, true);
        $criteria->compare('received', $this->received, true);
        $criteria->compare('jobno', $this->jobno, true);
        $criteria->compare('deptid', $this->deptid, true);
        $criteria->compare('jobclassid', $this->jobclassid, true);
        $criteria->compare('costype', $this->costype, true);
        $criteria->compare('entrytype', $this->entrytype);
        $criteria->compare('clearned', $this->clearned);
        $criteria->compare('dispatchno', $this->dispatchno, true);
        $criteria->compare('invoicetransid', $this->invoicetransid, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
}